<?php

namespace app\admin\controller;

use think\Controller;
use think\Loader;
use think\Request;
use think\Db;
use think\Session;
use PHPMailer\PHPMailer\PHPMailer;


class Login extends Controller
{
    public function index()
    {
        $list = db('ad_user')->field(['id','name','status'])->order('id','ASC')->select();

        return view('login/login', [
            'title' => '登陆页','list'=>$list
        ]);
    }

    public function regist()
    {
        return view('regist/regist',['title'=>'注册页']);
    }

    public function main()
    {
        $list = db('ad_user')->field(['id','name','status'])->order('id','ASC')->select();

        return view('main/index', [
            'title' => '登陆页','list'=>$list,'time'=>date('Y/m/d',time())
        ]);
    }

    public function email()
    {
            return view('login/email',['title'=>'邮箱注册']);
    }


    public function doemail()
    {
        $data['email'] = $_GET['email'];
        $data1 = $data['email'];
        $token = md5($data['email']);
        // $token1 = ($data['email']);
        Session::set('bbc',$data);
        // var_dump(Session::get('bbc'));die;
        $url = 'http://tp5.com/yanzheng.html?token'.$token;
        
       
        $mail = new PHPMailer();
try {
//Server settings
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.163.com';
$mail->Charset = 'UTF-8';
$mail->SMTPAuth = true;
$mail->Username = '13186525453@163.com';
$mail->Password = '88595832dtl';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
//Recipients
$mail->setFrom('13186525453@163.com', 'Mailer');
$mail->addAddress($data1, 'qq');
// $mail->addAddress('ellen@example.com');
// $mail->addReplyTo('info@example.com', 'Information
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
// //Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz');
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg');
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
// $mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->Body = 'This is 测试 <a href = '.$url.'>邮件</a>'; 
$mail->send();
echo '请去邮箱验证';


} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ';
}


    }



    public function yanzheng()
    {
        return view('regist/regist',['title'=>'详细信息']);
    }

    public function doregist()
    {
        // Request::instance()->session('bbc');
        // Session::start('bbc');
        $emaill=Session::get('bbc');
        
        // 调用validate 里的User类
        $validate = Loader::validate('User');
        // var_dump($validate);
        $name = $_POST['name'];
        $email = $emaill['email'];
        $tel = $_POST['tel'];
        $sex = $_POST['sex'];
        $pass = md5($_POST['pwd']);
        $repass = md5($_POST['pwd_confirm']);
        $time = date('Y/m/d',time());
        $pass1 = $_POST['pwd'];
        $repass1 = $_POST['pwd_confirm'];

       

        $data = [

                'name' => $name,
                'email' => $email,
                'tel' => $tel,
                'sex' => $sex,
                'pwd' => $pass1,
                'pwd_confirm' => $repass1,
                'createtime' => $time,
                'status' => '1'

        ];


        $data1 = [

                'name' => $name,
                'email' => $email,
                'tel' => $tel,
                'sex' => $sex,
                'pwd' => $pass,
                'createtime' => $time,
                'status' => '1'

        ];




        // $data1 = [
        //         'name' => $name,
        //         'tel' => $tel,
        //         'sex' => $sex,
        //         'pwd' => $pass,
        //         'createtime' => $time,
        //         'status' => '1'

        // ];

        // 正则验证匹配
        if(!$validate->check($data)){
            dump($validate->getError());
            // 不匹配返回错误信息
        }else{
            $result = db('ad_user')->insert($data1);
            if($result){
            $this->success('注册成功','admin/login/index');
        }
       }

   }

    //确认注册
    public function register(Request $request)
    {
        $p = $request->post();
//        var_dump($p)
        $data = [
            'name' => $p['name'],
            'pwd' => ($p['pwd'])
        ];
        $res = db('ad_user')->data($data)->insert();
        if ($res) {
//            return $this->success('注册成功', 'admin/index/login');
            return $this->redirect('admin/login/login');
        } else {
            return $this->error('注册失败');
        }
    }

    //注册页
    public function reg()
    {
        return view('login/register', [
            'title' => '注册页'
        ]);
    }

    //确认登陆
    public function dologin(Request $request)
    {
        $p = $request->post();
        $data = [
            'name' => $p['name'],
            'pwd' => md5(($p['pwd'])),
        ];

            // var_dump($data);die;
        
            Session::set('adname',$data['name']);
             $res = db('ad_user')
            ->where(['name'=>$data['name'],'pwd'=>$data['pwd']])
            ->find();

            // var_dump($row);die;
        
        // var_dump($stat);die;
       // var_dump($res);die;
        if ($res){

             $guanli = Session::get('adname');
        $dd = db('ad_user')->where('name','eq',$guanli)->field('id,status')->find();
        $uid = $dd['id'];
        $sta = $dd['status'];
//        dump($sta);
        $row = db('ad_user_role')
            ->alias('ur')
            ->field(['a.id','a.controller','a.access'])
            ->where('ur.user_id',$uid)
            ->join('mh_ad_role r','r.id=ur.role_id')
            ->join('mh_ad_role_access ra','r.id=ra.role_id')
            ->join('mh_ad_access a','ra.access_id=a.id')
            ->select();
            $arr = [];
//        var_dump($row);die;
        foreach ($row as $k=>$v){
            $arr[$v['controller']][] = $v['access'];
        }
        Session::set('name',$arr);

//        $a = Session::get('name');
            // var_dump($sta);die;

        if ($sta === 1){
            return view('index/index',[
                'guanli'=>$guanli
            ]);
        }else{
            return $this->error('您暂无权限登陆');
        }

    }


//            return $this->success('登陆成功', 'login/index/show');
            
    }

    


    
    }